package com.yz.common.factory;

import com.yz.common.properties.RedisProperties;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.time.Duration;

/**
 * Created With IntelliJ IDEA.
 * Descriptions:
 * <p>
 * User:Mr.Du
 * Date:2024/3/18
 * Time:19:46
 */
@Component
@RequiredArgsConstructor
public class JedisFactory {

    private final RedisProperties redisProperties;

    public Jedis getJedis(){
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        // 最大连接数
        jedisPoolConfig.setMaxTotal(30);
        // 最大空闲连接
        jedisPoolConfig.setMaxIdle(30);
        // 最小空闲连接
        jedisPoolConfig.setMinIdle(0);
        // 设置最长等待时间，ms
        jedisPoolConfig.setMaxWait(Duration.ofMillis(200));
        JedisPool jedisPool = new JedisPool(jedisPoolConfig, redisProperties.getHost(),
                redisProperties.getPort(),
                redisProperties.getTimeout(),
                redisProperties.getPassword());
        return jedisPool.getResource();
    }
}
